import types from '../types'
import common from '../common'
import store from '../index.js'
import axios from 'axios'
import moment from 'moment'
import env from '@/config/env'
import {apiGetRentUser, apiGetRentContList, apiNewRentCont} from '@/service/apiRent'
import {apiGetOrderList, apiUpdateOrder} from "@/service/apiRent"

const state = {
  storeUserRent: {
    /* contList: [
      contractId,   合同编号
      houseAddr,    房屋地址
      startDate,    租用开始日期
      endDate,      租用结束日期
      rental,       每月租金(分)
      payFreq,      租金缴纳方式
      auditStatus,  审核状态:A-录入待审核 S-审核通过 F-审核拒绝
    ] 
    */
    contList: null,         // 合同信息(列表)
    orderList: null,        // 订单信息(列表)
    httpCtlFlg: env.httpCltInit,
    renter: null,              // 是否为租客
    auditor: "0",             // 是否为审核员 0-普通租客会员 1-审核员 2-管理员 1和2有审核权限
    errmsg: null,
    errdesc: null
  }
}


const getters = {
  storeUserRent(state) {
    return state.storeUserRent;
  }
}

const actions = {
  async getRentUser({commit, state}, loginName) {
    apiGetRentUser(loginName, store.getters.authToken).then(res => {
      let resData = res.data;
      if (resData.subCode === env.ERRCD_SUCC) {
        commit(types.GET_RENTUSER, {isRetSucc:true, renter: resData.data});
      }
      else {
        commit(types.GET_RENTUSER, {isRetSucc:false, errcd: resData.subCode, errmsg: resData.subMsg});
      }
    }).catch(err => {
      console.log("err:", err);
      commit(types.GET_RENTUSER, {isRetSucc:false, errcd: "EHTTP", errmsg: "系统繁忙请重试"});
    });
  },
  async getContList({commit, state}, loginName) {
    apiGetRentContList(loginName).then(res => {
      let resData = res.data;
      // console.log("apiGetRentContList-resData:", resData);
      if (resData.subCode === env.ERRCD_SUCC) {
        let contList = [];
        if (resData.data !== null) {
          let list = resData.data;
          console.log("list:", list);
          for(let i = 0; i < list.length; i++) {
            // console.log("list[%d]:", i, list[i]);
            let row = {
              "contractId": list[i].contractId,
              "houseAddr": list[i].houseAddr,
              "startDate": list[i].startDate,
              "endDate": list[i].endDate,
              "rental": list[i].rental,
              "payFreq": list[i].payFreq,
              "auditStatus": list[i].auditStatus
            };
            contList.push(row);
          }
        }
        console.log("contList:", contList);
        commit(types.GET_CONTLIST, {isRetSucc:true, contList: contList});
      }
      else {
        commit(types.GET_CONTLIST, {isRetSucc:false, errcd: resData.subCode, errmsg: resData.subMsg});
      }
    }).catch(err => {
      console.log("err:", err);
      commit(types.GET_CONTLIST, {isRetSucc:false, errcd: "EHTTP", errmsg: "系统繁忙请重试"});
    });
  },
  async newRentCont({commit, state}, contInf) {
    state.storeUserRent.httpCtlFlg = env.HTTPCTL_SND;
    state.storeUserRent.errmsg = null;
    apiNewRentCont(contInf).then(res => {
      let resData = res.data;
      console.log("resData:", resData);
      if (resData.subCode !== env.ERRCD_SUCC) {
        commit(types.NEW_RENTCONT, {isRetSucc:false, errcd: resData.subCode, errmsg: resData.subMsg});
      }
      else {
        commit(types.NEW_RENTCONT, {isRetSucc:true});
      }
    }).catch(err => {
      console.log("err:", err);
      commit(types.NEW_RENTCONT, {isRetSucc:false, errcd: "EHTTP", errmsg: "系统繁忙请重试"});
    });
  },
  async getOrderList({commit, state}, loginName) {
    apiGetOrderList(loginName, store.getters.authToken).then(res => {
      let resData = res.data;
      if (resData.subCode === env.ERRCD_SUCC) {
        let orderList = [];
        if (resData.data !== null) {
          let list = resData.data;
          for(let i = 0; i < list.length; i++) {
            let row = {
              "odrId": list[i].odrId,
              "mchtOdrId": list[i].mchtOdrId,
              "odrDt": list[i].odrDt,
              "odrStatus": list[i].odrStatus,
              "odrAmt": list[i].odrAmt,
            };
            orderList.push(row);
          }
        }
        console.log("orderList:", orderList);
        commit(types.GET_ORDERLIST, {isRetSucc:true, orderList: orderList});
      }
      else {
        commit(types.GET_ORDERLIST, {isRetSucc:false, errcd: resData.subCode, errmsg: resData.subMsg});
      }
    }).catch(err => {
      console.log("err:", err);
      commit(types.GET_ORDERLIST, {isRetSucc:false, errcd: "EHTTP", errmsg: "系统繁忙请重试"});
    });
  },
  async updateOrder({commit, state}, orderInf) {
    state.storeUserRent.httpCtlFlg = env.HTTPCTL_SND;
    state.storeUserRent.errmsg = null;
    apiUpdateOrder(orderInf).then(res => {
      let resData = res.data;
      console.log("resData:", resData);
      if (resData.subCode !== env.ERRCD_SUCC) {
        commit(types.UPDATE_ORDER, {isRetSucc:false, errcd: resData.subCode, errmsg: resData.subMsg});
      }
      else {
        commit(types.UPDATE_ORDER, {isRetSucc:true});
      }
    }).catch(err => {
      console.log("err:", err);
      commit(types.UPDATE_ORDER, {isRetSucc:false, errcd: "EHTTP", errmsg: "系统繁忙请重试"});
    });
  }
}

const mutations = {
  [types.GET_RENTUSER](state, data) {
    if (data.isRetSucc) {
      state.storeUserRent.renter = data.renter;
      state.storeUserRent.auditor = data.renter.level;
    } else {
      state.storeUserRent.auditor = 0;
      state.storeUserRent.errmsg = data.errmsg;
      state.storeUserRent.errdesc = data.errdesc;
    }
  },
  [types.GET_CONTLIST](state, data) {
    if (data.isRetSucc) {
      state.storeUserRent.contList = data.contList;
    } else {
      state.storeUserRent.contList = null;
      state.storeUserRent.errmsg = data.errmsg;
      state.storeUserRent.errdesc = data.errdesc;
    }
  },
  [types.NEW_RENTCONT](state, data) {
    state.storeUserRent.httpCtlFlg = env.HTTPCTL_RET;
    if (!data.isRetSucc) {
      state.storeUserRent.errmsg = data.errmsg;
    }
  },
  [types.GET_ORDERLIST](state, data) {
    if (data.isRetSucc) {
      state.storeUserRent.orderList = data.orderList;
    } else {
      state.storeUserRent.contList = null;
      state.storeUserRent.errmsg = data.errmsg;
      state.storeUserRent.errdesc = data.errdesc;
    }
  },
  [types.UPDATE_ORDER](state, data) {
    state.storeUserRent.httpCtlFlg = env.HTTPCTL_RET;
    if (!data.isRetSucc) {
      state.storeUserRent.errmsg = data.errmsg;
    }
  }
}

export default {
  state,
  getters,
  actions,
  mutations
}
